<?php
namespace api\utils;
use herosphp\http\HttpClient;
use herosphp\string\StringUtils;
use herosphp\utils\AjaxResult;
use rsa\RSACrypt;
use rsa\SignUtil;

/**
 * 银行网关调用客户端
 * @author yangjian<yangjian102621@gmail.com>
 */
class BankAPIClient {

    //添加的头信息
    protected  static $HEADERS = array(
        'token' => 'bf5c1dd77201cd0d046c115f97feee2b',
        'Content-Type' => 'application/json; charset=utf-8'
    );


    /**
     * 发送api调用请求
     * @param $uri
     * @param $params
     * @return AjaxResult
     */
    public static function send($uri, $params) {

        $client = new HttpClient();
        $url = getConfig('bank_api_url').$uri;
//        $params['__timer'] = time();
//        $params['__secret'] = self::SECRET_KEY;
//        $sign = SignUtil::sign($url, $params);
//        $rsa = new RSACrypt();
//        $params['__sign'] = $rsa->encryptByPublicKey($sign);
        $client->setTimeout(120); //设置超时时间为120s
        $result =  $client->post($url, StringUtils::jsonEncode($params), self::$HEADERS);
        $result = StringUtils::jsonDecode($result);

        return new AjaxResult($result['code'], $result['message'], $result['data']);
    }
}
